package day_04;

import java.util.ArrayList;
import java.util.Scanner;

public class PDDFirst {
    private static final String[][] arrNumbers ={
            {null, "3456", "2367", "2367", "2367", "2367", "2367", "3456", null, null},
            {null, "45", "345", "45", "45", "45", "45", "3456", null, null},
            {null, "3456", "234567", "56", "45", "45", "34", "234567", null, null},
            {null, "3456", "2367", "67", "3456", "67", "2367", "3456", null, null},
            {null, "45", "345", "2345", "1245", "1234567", "45", "45", null, null},
            {null, "23", "234567", "23", "23456", "67", "2367", "3456", null, null},
            {null, "3456", "23", "23", "23456", "2367", "2367", "3456", null, null},
            {null, "234567", "234567", "67", "56", "45", "34", null, null},
            {null, "3456", "2367", "2367", "3456", "2367", "2367", "3456", null, null},
            {null, "3456", "2367", "2367", "34567", "67", "67", "3456", null, null}
    };

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = Integer.parseInt(sc.nextLine());
        ArrayList<Integer> retList = new ArrayList<>();
        // i 组数据
        for (int j = 0; j < i; j++) {
            int line = Integer.parseInt(sc.nextLine());
            int b = line / 10;
            if(b == 1){
                String[] ret = new String[line];
                for (int k = 0; k < line; k++) {
                    ret[k] = handle(sc.nextLine(), b);
                }
                retList.add(func(ret));
            }else{
                String[] ret = new String[10];
                int index = 0;
                for (int k = b - 1; k < line; k ++) {
                    String nextLine = sc.nextLine();
                    if(k % b == 0){
                        ret[index++] = handle(nextLine, b);
                    }
                }
                retList.add(func(ret));
            }
        }
        System.out.println();
        retList.forEach(System.out::println);
    }

    private static int func(String[] ret) {
        for (int i = 0; i < arrNumbers.length; i++) {
            String[] arrNumber = arrNumbers[i];
            if(equals(arrNumber, ret)){
                return i;
            }
        }
        return -1;
    }

    private static boolean equals(String[] one , String[] two){
        if(one.length != two.length) return false;
        for (int i = 0; i < one.length; i++) {
            if(one[i] == null){
                if(two[i] != null) return false;
            }else{
                if(!one[i].equals(two[i])) return false;
            }
        }
        return true;
    }

    private static String handle(String line, int b) {
        if(b == 1){
            String[] data = line.split(" ");
            StringBuffer sb = null;
            for (int i = 0; i < data.length; i++) {
                if(data[i].charAt(0) == '1'){
                    if(sb == null) sb = new StringBuffer();
                    sb.append(i);
                }
            }
            if(sb == null) return null;
            return sb.toString();
        }else{
            String[] data = line.split(" ");
            StringBuffer sb = null;
            for (int i = 0; i < data.length; i += b) {
                if(data[i].charAt(0) == '1'){
                    if(sb == null) sb = new StringBuffer();
                    sb.append(i / b);
                }
            }
            if(sb == null) return null;
            return sb.toString();
        }
    }
}
